package seentao.stdev.mobile.util;

import java.util.Map;

import lombok.SneakyThrows;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.context.ApplicationContext;

public class BeanUtil {

	@SneakyThrows
	public static <T> T getBean(ApplicationContext context, String className) {
		Class<?> clazz = Class.forName(className);
		return (T) context.getBean(clazz);
	}

	@SneakyThrows
	public static <T> T mapToBean(Map<String, Object>map, Class<T> type, String... ignore) {
		T bean = (T) type.newInstance();
		
		for (Map.Entry<String, Object> e : map.entrySet()) {
			boolean flag = true;
			String key = e.getKey();
			for (String s : ignore) {
				if (key.equals(s)) {
					flag = false;
					break;
				}
			}
			if (flag) {
				Object val = e.getValue();
				BeanUtils.copyProperty(bean, key, val);
			}
		}
		
		return bean;
	}

	@SneakyThrows
	public static <T> void setProperty(T bean, String name, Object value) {
		PropertyUtils.setProperty(bean, name, value);
	}
	
	@SneakyThrows
	public static <T> Object getProperty(T bean, String name) {
		return PropertyUtils.getProperty(bean, name);
	}

}
